import {
  getUserInfo,
  insertVipVisitorRecord
} from '../api/index.js'
import {
  SET_USER_CODE,
  SET_USER_INFO,
  SET_WX_USER_INFO
} from './mutation-types.js'


export default {
  //设置用用户code
  _setUserCode({ commit }, code){
    commit(SET_USER_CODE, code);
  },
  //设置微信返回的用户信息
  _setWXUserInfo({ commit }, WXuserInfo){
    commit(SET_WX_USER_INFO, WXuserInfo);
  },
  //设置后端返回的用户信息
  async _reqUserInfo({ state, commit }){
    let user_token = wx.getStorageSync('userToken');
    let params = {};
    if(!user_token){
      return;
    }else{
      params.user_token = user_token;
      let res = await getUserInfo(params);
      if(res.data.level == "success"){
        commit(SET_USER_INFO, res.data.data.user_info);
      }else{
        if(res.data.data == "no_binding"){
          wx.redirectTo({
            url: "/pages/homeMain/setPhone/main"
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      }
    }
  },
  //记录个人名片浏览量或者联系量
  async reqInsertVipVisitorRecord({commit},obj){
    let user_token = wx.getStorageSync('userToken');
    let params = {
      user_token: user_token,
      vip_user_id: obj.userId,
      type:obj.type
    };
    let res = await insertVipVisitorRecord(params);
    if(res.data.level == "success"){
    }else{
      wx.showToast({
        title: res.data.message,
        icon: 'none',
        duration: 1000
      })
    }
  }
}
